module.exports = {
   async $get_openId() {
        let openid= uni.getStorageSync('openid')
        if (openid) {
            return openid;
        } else{
            // #ifdef H5 
            let openid = location.search.split("?")[1]
            const res = await uni.$request({ openid }, "api/user/login")
            // #endif 
            // #ifndef H5
            const code = await uni.$get_code()
            const res = await uni.$request({ code }, uni.$openId_url)
            // #endif
            if (res.code==200)  {
                uni.setStorageSync('openid', res.data.openid || openid)
                // uni.setStorageSync('session_key', res.data.session_key)
                uni.setStorageSync('token', res.data.token)
                return res.data.openid;
            } else{
                // uni.navigateTo({
                //      url: '/tools/about/index'
                // });
            }
           
        }
    },
    $get_code(){
       
        const res = new Promise((success, fail) => {
            uni.login({
                provider: 'weixin',
                success: function (res) {
                    console.log(res);
                    success(res.code)
                },
                fail:(err)=>{
                    console.log(err);
                }
            });
        });
        return res;
    },
    /**
     * 
     * @param {*} service 
     * oauth	授权登录
     * share	分享
     * payment	支付
     * push	    推送
     */
    async $client(service ="oauth"){
        
        const res = new Promise((success, fail) => {
            uni.getProvider({
                service,
                success:(res)=>{
                    let client = res.provider[0] || "H5"
                    success(client)
                }
            })
        })
        return res;

    }
}